local CommonOHOH = _G.CommonOHOH;
local Text=CommonOHOH.config.Text
local TextColor = CommonOHOH.config.TextColor
local RunService = game:GetService("RunService")

local M ={};

M.Language="us" --默认英文


local Parting1="<--*LG1--*#~"
local Parting2="#--*LG2--*~"


local ServerIdKey="ID"
local ServerArgKey="Arg"
local ServerValueKey="Value"
local LanguageLoader = _G.CommonOHOH



function M.GetTxtBygId(self,id)
	return self.languageData:Translate(id)
end
function M.InitWorkSpaceText(self,obj)
	if not obj then
		return 
	end

	if obj:IsA("TextLabel") or obj:IsA("TextButton")  or obj:IsA("TextBox") then
		if obj:GetAttribute("LanguageId") then
			obj.Text=M:GetServerText(obj:GetAttribute("LanguageId"))
		end
		obj.AttributeChanged:Connect(function(attribute)
			if attribute=="LanguageId" then
				obj.Text=M:GetServerText(obj:GetAttribute("LanguageId"))
			end

			if attribute=="LanguageIdUpdate" then
				obj.Text=M:GetServerText(obj:GetAttribute("LanguageId"))
			end

		end)
		obj:SetAttribute("LanguageIdHadInit", true)
	end

	local child=obj:GetChildren()
	for i=1,#child do
		M:InitWorkSpaceText(child[i])
	end
end
function M.LocalizationBySeed(self,seed,callBack)
	if not RunService:IsStudio() then
		local a, b = pcall(function()
			self.languageData=LanguageLoader.LoadAsset(seed)
		end);
		if not a then
			return
		end
		if self:waitLoad(self.languageData) then
			return self.languageData
		elseif callBack then
			callBack(self.languageData)
		end
	end
end
function M.waitLoad(self,languageTable)
	local r=false
	if not languageTable then
		return
	end
	if not languageTable.GetChildren then
		return
	end

	for _, v in pairs(languageTable:GetChildren()) do
		if v:isA("ModuleScript") then
			local t=require(v)
			if t.Translate then
				r=t.Translate()
			end
		end
	end
	return r
end

function M.GetText2(self,id,values)	--文本id
	if values and #values>0 then
		local arg={}
		for i=1,#values do
			print("GetText2---->:%%ARG"..i)
			table.insert(arg,"%%ARG"..i)
		end
		return self:GetText(id,arg,values)
	else
		return self:GetText(id)
	end

end
function M.GetText(self,id,arg,values)	--文本id
	if not Text[id] then
		return "文本缺失："..id
	end
	local str=Text[id][M.Language]
	if arg and values then
		for i=1,#arg do
			str=string.gsub(str,arg[i],values[i],1) 
		end
	end
	----富文本处理----
	if Text[id].type ~= 0 and Text[id].type then
		assert(TextColor[Text[id].type].color,"没有对应文本颜色")
		str = "<font color=\""..TextColor[Text[id].type].color.."\">"..str.."</font>"
	end
	
	return str
end
function M.SetServerText(self,obj,str)
	obj:SetAttribute("LanguageId", str)
	if not obj:GetAttribute("LanguageIdUpdate") then
		obj:SetAttribute("LanguageIdUpdate", true)
	else
		obj:SetAttribute("LanguageIdUpdate", not obj:GetAttribute("LanguageIdUpdate"))
	end
end

--id,文本Id
--args 数组,可替换文本（可选）
--values 被替换文本的值，数组（可选）
function M.SendTextFormServer(self,id,args,values)
	local str=Parting1..ServerIdKey..Parting2..id

	if  args then
		for i,v in pairs(args) do
			str=str..Parting2..ServerArgKey..Parting2..v
		end
	end

	if values then
		for i,v in pairs(values) do
			str=str..Parting2..ServerValueKey..Parting2..v
		end
	end

	return str..Parting1
end

function M.GetServerText(self,str)

	local s1= string.split(str,Parting1)

	local s0=""
	for _,v in pairs(s1) do
		local s2=string.split(v,Parting2)
		local c=#s2
		if c>1 and s2[1]==ServerIdKey then
			local id= s2[2]
			local i=3
			local arg={}
			local values={}
			while(true) do
				if i>c then
					break
				end

				if s2[i]==ServerArgKey then
					i=i+1
					table.insert(arg,s2[i])
				end

				if s2[i]==ServerValueKey then
					i=i+1
					table.insert(values,s2[i])
				end

				i=i+1
			end
			local lstr=M:GetText(tonumber(id))
			for j=1,#arg do
				lstr=string.gsub(lstr,arg[j],values[j],1) 
			end
			s0=s0..lstr
		else
			s0=s0..v
		end

	end

	return s0
end

local HadTranslator={["zh"]=true}

function M.InitLanguage(self)
	--获取地区代码
	local lg= string.split(game:GetService("LocalizationService").RobloxLocaleId,"-")[1]
	
	-- warn("语言----》:",lg,game:GetService("LocalizationService").RobloxLocaleId)
	
	-- if  lg  and HadTranslator[lg] then
	-- 	M.Language="cn" --中文
	-- 	return
	-- end
	
	M.Language="us"-- 英文

end

-- M:LocalizationBySeed(13806634188)

return M
